// pagesA/waiters/waiters.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeadHeight: 0,
    navHeight: 0,
    selectIndex: 0,
    selectNum: 0,
    addresconter: '',
    address: '',
    isDefault: 1,
    regionId: null,
    avatar: '/static/images/personal/index/avatar.png',
    listType: [],
    servicesItem: null,
    totalPrice: 0,
    list: [{
        type: 0,
        name: '产后康复'
      },
      {
        type: 1,
        name: '计划术后'
      },
      {
        type: 2,
        name: '乳腺'
      },
    ],
    listnum: [{
        id: 1,
        text: '5天'
      },
      {
        id: 2,
        text: '7天'
      },
      {
        id: 3,
        text: '10天'
      },
    ],
    loading: true,
    isLock: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    qqmapsdk = new QQMapWX({
      key: app.globalData.key
    })
    if (wx.getStorageSync('addresconter') && wx.getStorageSync('address')) {
      this.setData({
        addresconter: wx.getStorageSync('addresconter'),
        address: wx.getStorageSync('address')
      })
    } else {
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: latitude,
              longitude: longitude
            },
            success: (res => {
              let resultData = res.result.address_component
              let addresconter = resultData.province + ' ' + resultData.city + ' ' + resultData.district
              wx.setStorageSync('addresconter', addresconter)
              let address = {
                province: resultData.province,
                city: resultData.city,
                areas: resultData.district
              }
              wx.setStorageSync('address', address)
            })
          })
        }
      })
    }
    let headHeight = 0
    wx.getSystemInfo({
      success: (res) => {
        headHeight = res.statusBarHeight
      }
    })
    // 获取胶囊坐标位置
    const res = wx.getMenuButtonBoundingClientRect()
    let menuInfoTop = res.top
    let menuInfoBottom = res.bottom
    this.setData({
      // navHeadHeight:(menuInfoTop+menuInfoBottom-headHeight)*2+'rpx',
      navHeadHeight: (res.top) * 2,
      navHeight: (res.height * 2)
    })
    this.getList()
  },
  getList: function () {
    let that = this
    wx.showLoading({
      mask: true
    })
    let newData = {
      areas: that.data.address.areas,
      city: that.data.address.city,
      province: that.data.address.province,

    }
    wx.request({
      url: app.globalData.serverUrl + '/services/items/detail/byRegionName',
      method: 'POST',
      data: newData,
      // header: {
      //   'token': wx.getStorageSync('token')
      // },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          if (resData.data.region.regionId == null) {
            that.setData({
              isDefault: 1,
              listType: resData.data.region.itemVoList,
              regionId: null,
              selectIndex: resData.data.region.itemVoList[0].type
            })
            that.servedetail()
          } else {
            that.setData({
              isDefault: 0,
              regionId: resData.data.region.regionId,
              listType: resData.data.region.itemVoList
            })
            that.servedetail()
          }
        } else if (resData.code == 5009) {
          wx.clearStorageSync()
          return wx.reLaunch({
            url: '/pages/login/login',
          })
        }
      })
    })
  },
  servedetail: function () {
    let that = this
    that.setData({
      loading: true
    })
    wx.showLoading({
      mask: true
    })
    let newData = {
      type: that.data.selectIndex,
      isDefault: that.data.isDefault,
      regionId: that.data.regionId
    }
    wx.request({
      url: app.globalData.serverUrl + '/services/items/detail',
      method: 'POST',
      data: newData,
      // header: {
      //   'token': wx.getStorageSync('token')
      // },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.servicesItem.servicesContent = resData.data.servicesItem.servicesContent.replace(/\<img/gi, '<img class="rich-img" ')
          that.setData({
            servicesItem: resData.data.servicesItem,
            selectNum: resData.data.servicesItem.serviceDetailList[0].serviceDetailId,
            totalPrice: resData.data.servicesItem.serviceDetailList[0].totalPrice,
            loading: false
          })
          wx.hideLoading()
        }
      })
    })
  },
  heldServeSelect(e) {
    this.setData({
      selectIndex: e.currentTarget.dataset.index
    })
    this.servedetail()
  },
  heldDaySelect(e) {
    this.setData({
      selectNum: e.currentTarget.dataset.index,
      totalPrice: e.currentTarget.dataset.sum
    })
  },
  heldLinkMone() {
    let that = this
    let newData = {
      type: that.data.selectIndex,
      isDefault: that.data.isDefault,
      regionId: that.data.regionId,
      servicesId: that.data.servicesItem.servicesId
    }
    wx.navigateTo({
      url: '/pagesA/waitersdetail/waitersdetail?servicesItem=' + JSON.stringify(newData)
    })
  },
  heldLinkPay() {
    let that = this
    let lock = null
    if (!that.data.isLock) {
      that.setData({
        isLock: new Date().getTime()
      })
    } else {
      lock = new Date().getTime()
    }
    if (that.data.isLock && lock) {
      if (lock - that.data.isLock < app.globalData.isNumLock) {
        return
      } else {
        that.setData({
          isLock: lock
        })
      }
    }
    if (!wx.getStorageSync('token')) {
      return wx.showModal({
        title: '提示',
        content: '您当前未登录，请先登录后再操作！',
        success(res) {
          if (res.confirm) {
            return wx.reLaunch({
              url: '/pagesA/loginI/loginI?tabNav=' + wx.getStorageSync('tabNav'),
            })
          } 
        }
      })
    }
    let newData = {
      type: that.data.selectIndex,
      isDefault: that.data.isDefault,
      regionId: that.data.regionId,
      servicesId: that.data.servicesItem.servicesId
    }
    wx.navigateTo({
      url: '/pagesA/waiterspay/waiterspay?servicesItem=' + JSON.stringify(newData) + '&type=' + this.data.selectIndex + '&serverId=' + this.data.selectNum
    })
  },
  // 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      isLock: null
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})